package com.example.demo.controller

import com.example.demo.dto.CelebrityDto
import com.example.demo.dto.CelebrityDto2
import com.example.demo.entity.Celebrity
import com.example.demo.repo.CelebrityRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
class CelebrityController {


    @Autowired
    private lateinit var celebrityRepo: CelebrityRepo

    @GetMapping("/celebrity")
    fun getCelebrities(): List<String> {
        return celebrityRepo.findByNameContains("").map { it.name }
    }

    @GetMapping("/celebrity/{name}")
    fun getCelebrities(@PathVariable name: String): List<String> {
        return celebrityRepo.findByNameContains(name).map { it.name }
    }

    @CrossOrigin("*")
    @PostMapping("/celebrity")
    fun addCelebrity(@RequestBody dto: CelebrityDto): Long? {
        return celebrityRepo.save(Celebrity(dto.name, "")).cid
    }

    @CrossOrigin("*")
    @PutMapping("/celebrity")
    fun editCelebrity(@RequestBody dto: CelebrityDto2) {
        celebrityRepo.save(Celebrity(dto.name, "", dto.id))
    }

    @CrossOrigin("*")
    @DeleteMapping("/celebrity/{id}")
    fun deleteCelebrity(@PathVariable id: Long) {
        return celebrityRepo.deleteById(id)
    }


}